//
//  JCEmotionTools.swift
//  SinaWeibo_18
//
//  Created by mac on 15/11/4.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

//一页表情最大行数
let PAGE_EMOTION_MAX_ROW = 3
//一页表情最大列数
let PAGE_EMOTION_MAX_COLUMN = 7
//一页最多表情数
let PAGE_EMOTION_TOTAL_COUNT = PAGE_EMOTION_MAX_ROW * PAGE_EMOTION_MAX_COLUMN - 1

class JCEmotionTools: NSObject {
    
    // MARK: - 确定每一种表情的范围
    
    //最近表情
    static let recentEmotionRange: NSRange = {
        return NSMakeRange(0,1)
    }()
    
    //默认表情范围
    static let defaultEmotionRange: NSRange = {
        
        let location = recentEmotionRange.length
        let length = (defaultEmotions.count - 1) / PAGE_EMOTION_TOTAL_COUNT + 1
       return NSMakeRange(location,length)
    }()
    //emoji表情范围
    static let emojiEmotionRange: NSRange = {
        
        let location = recentEmotionRange.length + defaultEmotionRange.length
        let length = (emojiEmotions.count - 1) / PAGE_EMOTION_TOTAL_COUNT + 1
        return NSMakeRange(location,length)
        }()
    //lxh表情范围
    static let lxhEmotionRange: NSRange = {
        
        let location = recentEmotionRange.length + defaultEmotionRange.length + emojiEmotionRange.length
        let length = (lxhEmotions.count - 1) / PAGE_EMOTION_TOTAL_COUNT + 1
        return NSMakeRange(location,length)
        }()
    
    //默认表情数组
    static let defaultEmotions: [JCEmotion] = {
       
      return JCEmotionTools.loadEmotions("EmotionIcons/default/info.plist")
    }()
    //emoji表情
    static let emojiEmotions: [JCEmotion] = {
        
        return JCEmotionTools.loadEmotions("EmotionIcons/emoji/info.plist")
    }()
    //lxh表情
    static let lxhEmotions: [JCEmotion] = {
        
        return JCEmotionTools.loadEmotions("EmotionIcons/lxh/info.plist")
        }()
    
    //计算总页数  12
    class func emotionsTotalPageCount() -> Int {
        
//        let recentEmotionPages = 1
//        let defaultEmotionPages = (defaultEmotions.count-1) / 20 + 1
//        let emojiEmotionPages = (emojiEmotions.count - 1) / 20 + 1
//        let lxhEmotionPages = (lxhEmotions.count - 1) / 20 + 1
        let recentEmotionPages = recentEmotionRange.length
        let defaultEmotionPages = defaultEmotionRange.length
        let emojiEmotionPages = emojiEmotionRange.length
        let lxhEmotionPages = lxhEmotionRange.length
        
        return defaultEmotionPages + emojiEmotionPages + lxhEmotionPages + recentEmotionPages
    }
    
    //查看当前页数是在哪一种表情范围之内
    class func emotionType(pageNum: Int) -> JCEmotionToolBarButtonType{
        
        if pageNum == recentEmotionRange.location {
            return .Recent
        }else if (NSLocationInRange(pageNum, defaultEmotionRange)) {
            return .Default
        }else if (NSLocationInRange(pageNum, emojiEmotionRange)){
            return .Emoji
        }else {
            return .Lxh
        }
        
    }
    
    //根据表情类型 获取在collectionview得第几页
    class func pageNumWithEmotionType(type: JCEmotionToolBarButtonType) -> Int {
        switch type {
        case .Recent:
            return recentEmotionRange.location
        case .Default:
            return defaultEmotionRange.location
        case .Emoji:
            return emojiEmotionRange.location
        case .Lxh:
            return lxhEmotionRange.location
        }
    }
    
    //根据表情类型,判断该表情共有多少页
    class func pageControlTotalPages(type: JCEmotionToolBarButtonType) -> Int {
        switch type {
        case .Recent:
            return recentEmotionRange.length
        case .Default:
            return defaultEmotionRange.length
        case .Emoji:
            return emojiEmotionRange.length
        case .Lxh:
            return lxhEmotionRange.length
        }
    }
    
    //计算当前滚动到某种表情的第几页
    class func pageControlCurrentPage(page: Int) -> Int {
        let type = emotionType(page)
        
        switch type {
        case .Recent:
            return recentEmotionRange.location
        case .Default:
            return page - recentEmotionRange.length
        case .Emoji:
            return page - (recentEmotionRange.length + defaultEmotionRange.length)
        case .Lxh:
            return page - (recentEmotionRange.length + defaultEmotionRange.length + emojiEmotionRange.length)
        }
    }
    
    //通过当前第几页 来获取对应表情的页数(截取plist里的表情数据)
    class func subEmotionsWithPageNum(pageNum: Int) -> [JCEmotion] {
        
        func subEmotionsWithPageNum(pageNum: Int, emotions: [JCEmotion]) -> [JCEmotion] {
            //获取在当前表情的页数
            let currentEmotionPage = pageControlCurrentPage(pageNum)
            //从哪儿截取
            let location = currentEmotionPage * 20
            //截取多长数据
            var length = PAGE_EMOTION_TOTAL_COUNT
            //判断是否数组越界
            if (location + length) > emotions.count {
                //最后一页长度需判断
                length = emotions.count - location
            }
            //要截取的位置
            let range = NSMakeRange(location, length)
            //截取子数组
            let result = (emotions as NSArray).subarrayWithRange(range)
//            printLog(result)
            //返回模型
            return result as! [JCEmotion]
        }
        
        //根据当前页  判断是在那个表情模型中
        let type  = JCEmotionTools.emotionType(pageNum)
        switch type {
        case .Recent:
            return [JCEmotion]()
        case .Default:
            return subEmotionsWithPageNum(pageNum, emotions: defaultEmotions)
        case .Emoji:
            return subEmotionsWithPageNum(pageNum, emotions: emojiEmotions)
        case .Lxh:
            return subEmotionsWithPageNum(pageNum, emotions: lxhEmotions)
        }
    }
    
    //从plist文件中读取数据
    private class func loadEmotions(pathStr:String) -> [JCEmotion]{
        
        let path = NSBundle.mainBundle().pathForResource(pathStr, ofType: nil)
        //表情数据 字典
        let array = NSArray(contentsOfFile: path!)
        
        var result = [JCEmotion]()
        //遍历添加模型
        for value in array!{
            
            let emotion = JCEmotion(dict: value as! [String: AnyObject])
            //获取图片前缀名称
            emotion.prepath = (pathStr as NSString).stringByDeletingLastPathComponent
            
            result.append(emotion)
        }
        
        return result
    }
}
